package org.linlinjava.litemall.admin.controller;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.dto.LitemallOrderDto;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理litemall顺序控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/lumiere/subscribeOrder")
@Validated
public class AdminLitemallOrderController {


    /**
     * 卢米埃订阅用户订单服务
     */
    @Autowired
    private LumiereSubscribeUserOrderService lumiereSubscribeUserOrderService;

    /**
     * litemall订货服务
     */
    @Autowired
    private LitemallOrderGoodsService litemallOrderGoodsService;

    /**
     * 查询列表
     *
     * @param name          名字
     * @param orderSn       订单sn
     * @param shopId        商店id
     * @param userName      用户名
     * @param goodsName     商品名称
     * @param orderType     订单类型
     * @param status        状态
     * @param subscribeName 订阅名字
     * @param page          页面
     * @param limit         限制
     * @param sort          排序
     * @param order         订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object queryList(String name, String orderSn, Integer shopId,String userName,
                            String goodsName,Byte orderType, Short status,
                            String subscribeName,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @Sort @RequestParam(defaultValue = "add_time") String sort,
                            @Order @RequestParam(defaultValue = "desc") String order) {
//        List<LitemallOrder> list = adminLitemallOrderService.queryList(shopId, orderSn, page, limit, sort, order,status,orderType);
          List<LitemallOrderDto> dtos = lumiereSubscribeUserOrderService.getUserLogByConditions(name, orderSn, shopId, userName, goodsName, orderType, status, subscribeName, page, limit);
//        list = list.stream().filter(

//                o -> {
//                    LumiereSubscribe subscribe = lumiereSubscribeService.findById(o.getExternalId());
//                    boolean accountFlag = true;
//
//
//                    if (subscribeName != null) {
//                        accountFlag = subscribe.getName().indexOf(subscribeName) == -1 ? false : true && accountFlag;
//                    }
//
//
//
//                    return accountFlag;
//                }).collect(Collectors.toList());
////       }
//
        dtos = dtos.stream().map(litemallOrder -> {
                    boolean flag = false;

                    LitemallOrderDto orderDto = new LitemallOrderDto();
                    BeanUtils.copyProperties(litemallOrder, orderDto);

                    //获取订单对应的商品名称
                    List<LitemallOrderGoods> goods = litemallOrderGoodsService.queryByOid(litemallOrder.getId());

                    //商品
                    if (goodsName != null && goods.size() > 0) {
                        for (int i = 0; i < goods.size(); i++) {
                            if (goods.get(i).getGoodsName().indexOf(goodsName) != -1) {
                                flag = true;
                                break;
                            }
                        }
                    } else {
                        flag = true;
                    }
                    orderDto.setGoods(goods);

                    String receiptRange = litemallOrder.getReceiptStartTime() + "--" + litemallOrder.getReceiptEndTime();
                    orderDto.setReceiptRange(litemallOrder.getReceiptStartTime() != null || litemallOrder.getReceiptEndTime() != null ? receiptRange : null);
                return orderDto;
        }).collect(Collectors.toList());
        return ResponseUtil.okList(dtos);
    }


}
